package club.mrxiao.gps.socket.util;

import club.mrxiao.gps.socket.domain.Gt06InputDTO;

/**
 * 协议处理
 * @author  xiaoyu
 *
 */
public class ProtocolUtil {

	/**
	 * 服务器响应数据构造
	 * @param protocolNumber 协议号
	 * @param seriesNumber 信息序列号
	 * @return
	 */
	public static String serverResponseData(String protocolNumber,String seriesNumber){
		if(!StringUtil.isNullStr(seriesNumber) && !StringUtil.isNullStr(protocolNumber) ){
			String start = Gt06InputDTO.START_BIT;
			String length = String.format("%02x", 5).toUpperCase();
			String hexStr = length + protocolNumber + seriesNumber;
			Crc16Util crc16 = new Crc16Util();
			crc16.reset();
    		crc16.update(Byte2Hex.hexString2Bytes(hexStr));
    		String crc = Integer.toHexString(crc16.getCrcValue()).toUpperCase();
            return start+length+protocolNumber+seriesNumber+crc+Gt06InputDTO.END_BIT;
		}
		return null;
	}

	/**
	 * 服务器响应数据构造
	 * @param protocolNumber  协议号
	 * @param context 信息内容
	 * @param seriesNumber 信息序列号
	 * @return
	 */
	public static String serverResponseData(String protocolNumber, String context, String seriesNumber) {
		if (!StringUtil.isNullStr(seriesNumber) && !StringUtil.isNullStr(protocolNumber)) {
			String start = Gt06InputDTO.START_BIT;
			String length = String.format("%02x", 5 + context.length()).toUpperCase();
			String hexStr = length + protocolNumber + context + seriesNumber;
			Crc16Util crc16 = new Crc16Util();
			crc16.reset();
			crc16.update(Byte2Hex.hexString2Bytes(hexStr));
			String crc = Integer.toHexString(crc16.getCrcValue()).toUpperCase();
			return start + length + protocolNumber + context + seriesNumber + crc + Gt06InputDTO.END_BIT;
		}
		return null;
	}
}